package org.dromara.business.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.business.domain.vo.UserInfoWebExtVo;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.domain.vo.UserInfoVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.business.domain.vo.UserInfoWebVo;
import org.dromara.business.domain.bo.UserInfoWebBo;
import org.dromara.business.service.IUserInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 前台用户
 *
 * @author Hao Tong Xue
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/userInfo")
public class UserInfoController extends BaseController {

    private final IUserInfoService userInfoService;

    /**
     * 查询前台用户列表
     */
//    @SaCheckPermission("business:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo<UserInfoWebVo> list(UserInfoWebBo bo, PageQuery pageQuery) {
        return userInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出前台用户列表
     */
//    @SaCheckPermission("business:userInfo:export")
    @Log(title = "前台用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserInfoWebBo bo, HttpServletResponse response) {
        List<UserInfoWebVo> list = userInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "前台用户", UserInfoWebVo.class, response);
    }

    /**
     * 获取前台用户详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("business:userInfo:query")
    @GetMapping("/{id}")
    public R<UserInfoWebVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userInfoService.queryById(id));
    }

    /**
     * 新增前台用户
     */
//    @SaCheckPermission("business:userInfo:add")
    @Log(title = "前台用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserInfoWebBo bo) {
        return toAjax(userInfoService.insertByBo(bo));
    }

    /**
     * 修改前台用户
     */
//    @SaCheckPermission("business:userInfo:edit")
    @Log(title = "前台用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserInfoWebBo bo) {
        return toAjax(userInfoService.updateByBo(bo));
    }

    /**
     * 删除前台用户
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("business:userInfo:remove")
    @Log(title = "前台用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取用户信息
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public R<UserInfoWebVo> getWebInfo() {
        return R.ok(userInfoService.getInfo());
    }

    /**
     * 获取用户卡片信息
     * @param uId 用户id
     * @return 用户信息
     */
    @GetMapping("/getCardUInfo/{uId}")
    public R<UserInfoWebVo> getCardUInfo(@PathVariable Long uId) {
        return R.ok(userInfoService.getCardUInfo(uId));
    }

    /**
     * 获取用户附加扩展信息
     * @param uId 用户id
     * @return 附加扩展信息
     */
    @GetMapping("/getUserInfoExt/{uId}")
    public R<UserInfoWebExtVo> getUserInfoExt(@PathVariable Long uId) {
        return R.ok(userInfoService.getUserInfoExt(uId));
    }

}
